iT邦幫忙

2021 iThome 鐵人賽

DAY 21
0
Mobile Development

Flutter - 從 Packages & Plugins 掌握原生系列 第 24

Day24 Plugin 從零開始到上架 - FlutterPlugin與 MethodCallHandler

  • 分享至 

  • xImage
  •  

InstagramBasicDisplayApiPlugin

class InstagramBasicDisplayApiPlugin : FlutterPlugin, ActivityAware {

  private lateinit var methodCallHandler: MethodCallHandlerImpl
  private val instagramBasicDisplayApi: InstagramBasicDisplayApi = getKoinInstance()

  override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
    methodCallHandler = MethodCallHandlerImpl(instagramBasicDisplayApi)
    methodCallHandler.startListening(flutterPluginBinding.binaryMessenger)
  }

  override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
    methodCallHandler.stopListening()
  }

  override fun onAttachedToActivity(binding: ActivityPluginBinding) {
    instagramBasicDisplayApi.setActivityPluginBinding(binding)
  }

  override fun onDetachedFromActivity() {
    instagramBasicDisplayApi.detachActivity()
  }

  override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
    onAttachedToActivity(binding)
  }

  override fun onDetachedFromActivityForConfigChanges() {
    onDetachedFromActivity()
  }
}

MethodCallHandlerImpl


class MethodCallHandlerImpl(private val instagramBasicDisplayApi: InstagramBasicDisplayApi) :
    MethodChannel.MethodCallHandler {

    private val TAG = javaClass.name
    private var channel: MethodChannel? = null

    init {
        instagramBasicDisplayApi.startListening(
            userUpdated = {
                channel?.invokeMethod("userUpdated", hashMapOf("ID" to it.id, "USER_NAME" to it.username, "ACCOUNT_TYPE" to it.accountType))
            },
            mediasUpdated = {
                channel?.invokeMethod("mediasUpdated", hashMapOf("DATA" to it))
            },
            albumDetailUpdated = {
                channel?.invokeMethod("albumDetailUpdated", hashMapOf("DATA" to it))
            },
            mediaItemUpdated = {
                channel?.invokeMethod("mediaItemUpdated", it)
            },
            errorUpdated = {
                channel?.invokeMethod("errorUpdated", hashMapOf("ERROR_TYPE" to it))
            }
        )
    }

    override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: MethodChannel.Result) {
        when (call.method) {
            "getPlatformVersion" -> {
                result.success("Android ${android.os.Build.VERSION.RELEASE}")
            }
            "askInstagramToken" -> {
                instagramBasicDisplayApi.askInstagramToken()
                result.success(null)
            }
            "getInstagramUser" -> {
                instagramBasicDisplayApi.getInstagramUser()
                result.success(null)
            }
            "getMedias" -> {
                instagramBasicDisplayApi.getMedias()
                result.success(null)
            }
            "getAlbumDetail" -> {
                println("getAlbumDetail\n")
                val arguments = call.arguments as? HashMap<*, *> ?: return
                val albumId = arguments["albumId"] as String? ?: return
                println("getAlbumDetail albumId = $albumId")
                instagramBasicDisplayApi.getAlbumDetail(albumId)
                result.success(null)
            }
            "getMediaItem" -> {
                println("getMediaItem\n")
                val arguments = call.arguments as? HashMap<*, *> ?: return
                val mediaId = arguments["mediaId"] as String? ?: return
                println("getMediaItem mediaId = $mediaId")
                instagramBasicDisplayApi.getMediaItem(mediaId)
                result.success(null)
            }
            "logout" -> {
                instagramBasicDisplayApi.logout()
                result.success(null)
            }
            else -> {
                result.notImplemented()
            }
        }
    }

    fun startListening(messenger: BinaryMessenger?) {
        if (channel != null) {
            Log.wtf(TAG, "Setting a method call handler before the last was disposed.")
            stopListening()
        }
        channel = MethodChannel(messenger, "instagram_basic_display_api").apply {
            setMethodCallHandler(this@MethodCallHandlerImpl)
        }
    }

    fun stopListening() {
        if (channel == null) {
            Log.d(TAG, "Tried to stop listening when no MethodChannel had been initialized.")
            return
        }
        channel!!.setMethodCallHandler(null)
        channel = null
    }
}

上一篇
Day23 Plugin 從零開始到上架 - ActivityAware 介紹
下一篇
Day25 Plugin 從零開始到上架 - Android instagram APIs
系列文
Flutter - 從 Packages & Plugins 掌握原生30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言